<?php
/**
 * LinPHP Core
 */

class Lin_Application
{
	public static $resouces = array();
	
	public function __construct()
	{
		$this->getDefaultResouce();
	}
	
	/**
	 * 启用系统默认需要的资源
	 */
	private function getDefaultResouce()
	{
		
		//默认配置文件
		$this->getConfig('config');
		
		//库,模块资源
		$this->regResource( array('Lin' => SITE_ROOT . '/library/Lin'))
				->regResource( array('Model' => APPLICATION_PATH . '/models'))
				->regResource( array('Modules' => APPLICATION_PATH . '/modules'));
		
	}
	
	/**
	 * 
	 * 程序运行之前，用户初始化资源
	 */
	public function bootstrap()
	{	
		return $this;	
	}	
	
	/**
	 * 
	 * 程序开始
	 * @param none
	 * @return void 
	 */
	public function run()
	{
		$router = new Lin_Router();
		$router->run();		
	}	
		
	/**
	 * 资源注册
	 * 
	 * @param array
	 * @return void
	 */
	public function regResource($resource = array())
	{	
		list($resourceName, $path) = each($resource);
		if (!isset(self::$resouces[$resourceName]))
		{
			self::$resouces[$resourceName] = $path;	
		}
		return $this; 		
	} 
	
	/**
	 * 获取配置文件
	 */
	public function getConfig($fileName)
	{
		if (file_exists($file = APPLICATION_PATH . '/configs/' . $fileName . '.php'))
		{
			require_once ($file);
		}
		else
		{
			die('Cann\' get config file[' . $file. ']');
		}
	}
}

/**
 * 自动加载类资源
 * 
 * @param string
 * @return void
 */
function __autoload($class)
{
	if (strstr($class, '_'))
	{
		$tmp = explode('_', $class);
		$resourceName = array_shift($tmp);
		$path = implode('/', $tmp);
		if (!Lin_Application::$resouces[$resourceName])
		{
			die('<p>Cann\'t get resource[' . $resourceName . ']!</p>');
			return false;
		}
		else 
		{
			$file = Lin_Application::$resouces[$resourceName] . "/{$path}.php";
			if (!file_exists($file))
			{
				die('<p>File['. $file . '] does not exist!</p>');
			}
			require_once $file;
		}
		
	}
}